using B.S.RbacData.ErrorCode;
using ContentmanagementBase.Domain.Enums;
using ContentmanagementBaseRead.API.Applications.Commands.Popularizations;
using ContentmanagementBaseRead.API.Dtos;
using MediatR;
using System.ComponentModel;
using System.Reflection;

namespace ContentmanagementBaseRead.API.Applications.Commandhandlters.Popularizations
{
    /// <summary>
    /// 获取医疗科室列表命令处理器
    /// </summary>
    public class GetMedicalDepartmentsCommandHandler : IRequestHandler<GetMedicalDepartmentsCommand, ApiResult<List<MedicalDepartmentDto>>>
    {
        /// <summary>
        /// 日志记录器
        /// </summary>
        private readonly ILogger<GetMedicalDepartmentsCommandHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志记录器</param>
        public GetMedicalDepartmentsCommandHandler(ILogger<GetMedicalDepartmentsCommandHandler> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 处理获取医疗科室列表命令
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        public async Task<ApiResult<List<MedicalDepartmentDto>>> Handle(GetMedicalDepartmentsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogInformation("开始处理获取医疗科室列表命令");

                var departments = new List<MedicalDepartmentDto>();

                // 使用反射获取枚举值和描述
                var enumType = typeof(MedicalDepartmentEnum);
                var enumValues = Enum.GetValues(enumType);

                foreach (var enumValue in enumValues)
                {
                    var value = (int)enumValue;
                    var name = enumValue.ToString();
                    
                    // 获取Description特性
                    var fieldInfo = enumType.GetField(name);
                    var descriptionAttribute = fieldInfo?.GetCustomAttribute<DescriptionAttribute>();
                    var description = descriptionAttribute?.Description ?? name;

                    departments.Add(new MedicalDepartmentDto
                    {
                        Value = value,
                        Name = name,
                        Description = description
                    });
                }

                _logger.LogInformation("医疗科室列表获取成功，共 {Count} 个科室", departments.Count);

                return new ApiResult<List<MedicalDepartmentDto>>
                {
                    code = ResultEnumCode.Ok,
                    message = "医疗科室列表获取成功",
                    data = departments
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取医疗科室列表过程中发生异常");
                return new ApiResult<List<MedicalDepartmentDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取医疗科室列表异常：{ex.Message}",
                    data = new List<MedicalDepartmentDto>()
                };
            }
        }
    }
} 